package com.hhd.listener;

import com.alibaba.fastjson.JSONObject;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.parser.PdfReaderContentParser;
import com.itextpdf.text.pdf.parser.TextMarginFinder;
import com.hhd.pdf.GeneratorParameters;
import com.hhd.pdf.PdfGenerator;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;

@Component
public class TestListener implements ApplicationListener<ContextRefreshedEvent> {

    @Autowired
    PdfGenerator pdfGenerator;

    @SneakyThrows
    @Override
    public void onApplicationEvent(ContextRefreshedEvent event) {
        test1();
//        test2();
//        test3();
//        test4();
//        test5();
    }

    private void test5() {
        JSONObject json = JSONObject.parseObject("{\"meetingId\":\"\",\"status_dictText\":\"已完成\",\"applyDept\":\"2078486910858338304\",\"updateBy\":\"18701235126\",\"unitId\":\"35791748244a25603465c3b7e1c95eb7\",\"id\":\"47827b93399edbef1040234a32fe70fe\",\"applyTime\":\"2024-01-05\",\"executeWay\":\"1\",\"applyBy\":\"aebec65c2edeaa7faf6fa63945d681ff\",\"taskMeta\":{\"processInstanceId\":\"157f0b13-ab73-11ee-9615-e094677abc9b\",\"members\":[],\"memberNames\":[],\"active\":false,\"taskName\":\"财务分管领导\",\"assignee\":\"aebec65c2edeaa7faf6fa63945d681ff\",\"taskId\":\"617e67943c39dbce7d5d0316fc43e204\",\"needsSign\":false,\"notify\":false},\"summary\":\"去外地去外地\",\"issueId\":\"\",\"def1\":\"wdqwfqwd\",\"def2\":\"1111\",\"applyDept_dictText\":\"研发中心\",\"isMeeting\":\"0\",\"applyByName\":\"宋曼\",\"executeWay_dictText\":\"直批流程\",\"updateTime\":\"2024-01-05\",\"receiptCode\":\"XMYS20240090\",\"createBy\":\"18701235126\",\"projectReceiptModelList\":[{\"projectFileList\":[],\"projectStatus\":\"8\",\"receiptBy\":\"唐泽宇\",\"attachments\":[],\"projectCode\":\"XM20230917007\",\"receiptDate\":\"2024-01-05\",\"startTime\":\"2023\",\"projectStatus_dictText\":\"已验收\",\"projectName\":\"立项修改测试，勿使用该单据_gw\",\"projectId\":\"7b2cf7baed0ebea1650d95782d8b43be\"}],\"applyDeptName\":\"研发中心\",\"createTime\":\"2024-01-05\",\"isTriggerDiscussDecide\":\"0\",\"sysOrgCode\":\"A01\",\"status\":\"3\"}");

        GeneratorParameters parameters = GeneratorParameters.builder()
                .configPath("config-prj-accept.xml")
                .outputPath("G:\\temp\\2023年12月25日\\2.pdf")
                .renderData(json)
                .build().init();
        pdfGenerator.generate(parameters);
    }

    private void test4() throws IOException {
        PdfReader pdfReader = new PdfReader("G:\\temp\\2023年12月25日\\1.pdf");
        PdfReaderContentParser parser = new PdfReaderContentParser(pdfReader);
        TextMarginFinder textMarginFinder = parser.processContent(1, new TextMarginFinder());
        float height = textMarginFinder.getHeight();
        System.out.println(height);
    }


    private void test3() {
        GeneratorParameters parameters = GeneratorParameters.builder()
                .configPath("config-plan.xml")
                .outputPath("G:\\temp\\2023年12月25日\\1.pdf")
                .renderData(readData("JSONData-plan"))
                .build().init();
        pdfGenerator.generate(parameters);
    }

    private void test2() {
        GeneratorParameters parameters = GeneratorParameters.builder()
                .configPath("config-project.xml")
                .outputPath("G:\\temp\\2023年12月25日\\1.pdf")
                .renderData(readData("JSONData-project"))
                .build().init();
        pdfGenerator.generate(parameters);
    }

    private void test1() {
        GeneratorParameters parameters = GeneratorParameters.builder()
                .configPath("config.xml")
                .outputPath("G:\\temp\\2023年12月25日\\1.pdf")
                .renderData(readData("JSONData"))
                .build().init();
        pdfGenerator.generate(parameters);
    }

    private JSONObject readData(String fileName) {
        try (InputStream inputStream = this.getClass().getResourceAsStream("/" + fileName + ".json")) {
            byte[] bytes = new byte[inputStream.available()];
            inputStream.read(bytes);
            return JSONObject.parseObject(new String(bytes, StandardCharsets.UTF_8));
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    private JSONObject createData() {
        try (InputStream resourceAsStream = this.getClass().getClassLoader().getResourceAsStream("/JSONData.json")) {
            byte[] bytes = new byte[resourceAsStream.available()];
            resourceAsStream.read(bytes);
            return JSONObject.parseObject(new String(bytes, StandardCharsets.UTF_8));
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
